package com.example.edu.file;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileMain {

    public static void main(String[] args) throws IOException {
        final String userDir = System.getProperty("user.dir");
        globMatch(userDir, "*.java");
    }

    public static void globMatch(String baseDir, String globMatcher) throws IOException {
        FileMatchingVisitor fileMatchingVisitor = new FileMatchingVisitor(globMatcher);
        final Path basePath = Paths.get(baseDir);
        if (!FileMain.isExists(basePath)) {
            System.out.println("no such file " + basePath + ", skip this path.");
            return;
        }

        Files.walkFileTree(basePath, fileMatchingVisitor);
        fileMatchingVisitor.done();
        return;
    }

    public static void createDirsIfNotExists(Path path) throws IOException {
        if (path == null) {
            return;
        }
        if (!Files.exists(path)) {
            Files.createDirectories(path);
        }
    }

    public static void createFileIfNotExists(Path path) throws IOException {
        if (!Files.exists(path)) {
            createDirsIfNotExists(path.getParent());
            Files.createFile(path);
        }
    }

    public static boolean isExists(Path path) {
        return Files.exists(path);
    }

}
